<?php

/**
*文件夹下的图片去除水印然后打印文字水印
*/
function getStamp($path,$path_new)
{
    //$path = 'E:/php/calligraphy/picture-test/1-3/1d832ae7ee4cb7b80b49e5ac146be439.jpg';
    $image = file_get_contents($path);
    $info = getimagesize($path);
    $im = imagecreatefromstring($image);
    $width = $info[0];
    $height = $info[1];
    for($i=0;$i<$height;$i+=1){
	    for($j=0;$j<$width;$j+=1){
            $rgb = ImageColorAt($im, $j, $i);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            //echo $r.'.'.$g.'.'.$b.'.='.$rgb.'<br>x='.$j.', y='.$i.'<br>';
            //i竖  j横
            if ($i > 640 && $j > 530) {
            //米黄色
                $randx = rand(5,30);
                $randy = rand(0,38);
                $randz = rand(10, 40);
                $hex = imagecolorallocate($im, $randx, $randy, $randz);
                imagesetpixel($im,$j, $i, $hex);
            }
        }
    }
    $color = imagecolorallocate($im, 255, 255, 255);
    $font_file = 'E://php/calligraphy/picture-test/1-3/01/font/simsun.ttc';
    imagefttext($im, 20, 0, 545, 670, $color, $font_file, '"流谦书苑"');
    imagepng($im, $path_new);

 

  return true;
}

//扫描文件夹下的搜所有图片
$dir_files = "../../../../beitie_re/1/3/"; 
$dir_file_news = "../../../../beitie_re_watermark/1/3/";
if(!is_dir($dir_file_news)) {
    mkdir($dir_file_news, 0777, true);
}
$dir_file_list = scandir($dir_files);
//$dir_file_list = glob("../../../../beitie_re/1/3/*.jpg");
//var_dump($dir_file_list);
foreach ($dir_file_list as $key => $value) {
	$suffix = substr($value, -3);
	if ($suffix == 'jpg') {
		$path = $dir_files . "/" . $value;
		$path_new = $dir_file_news . "/" . $value;
        getStamp($path, $path_new);
	}
}

